/**
 * File    : ApprovalManagerImpl.java
 * Created : 2008-10-10
 * By      : LuHuiguo
 */
package com.luhuiguo.sso.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.transaction.annotation.Transactional;

import com.luhuiguo.sso.dao.ApprovalDao;
import com.luhuiguo.sso.entity.Approval;
import com.luhuiguo.sso.entity.Persona;
import com.luhuiguo.sso.entity.Site;
import com.luhuiguo.sso.entity.User;
import com.luhuiguo.sso.service.ApprovalManager;
import com.luhuiguo.sso.service.PersonaManager;
import com.luhuiguo.sso.service.SiteManager;

/**
 * @author LuHuiguo
 * @version $Id: ApprovalManagerImpl.java 9 2008-10-12 12:51:20Z LuHuiguo $
 * 
 */
public class ApprovalManagerImpl implements ApprovalManager {

	private ApprovalDao approvalDao;

	private SiteManager siteManager;

	private PersonaManager personaManager;

	@Required
	public void setApprovalDao(ApprovalDao approvalDao) {
		this.approvalDao = approvalDao;
	}

	@Required
	public void setPersonaManager(PersonaManager personaManager) {
		this.personaManager = personaManager;
	}

	@Transactional(readOnly = true)
	@Override
	public Approval getApproval(Long id) {
		return approvalDao.get(id);
	}

	@Transactional(readOnly = true)
	@Override
	public Approval getApproval(User user, String url) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("user", user);
		params.put("url", url);
		return approvalDao.findFirstByNamedQueryAndNamedParams(
				"approval.byUserAndUrl", params);
	}

	@Transactional(readOnly = true)
	@Override
	public Approval getApproval(User user, Site site) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("user", user);
		params.put("site", site);
		return approvalDao.findFirstByNamedQueryAndNamedParams(
				"approval.byUserAndSite", params);

	}

	@Transactional(readOnly = true)
	@Override
	public Approval getApproval(String username, String url) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("username", username);
		params.put("url", url);
		return approvalDao.findFirstByNamedQueryAndNamedParams(
				"approval.byUsernameAndUrl", params);

	}

	@Transactional(readOnly = true)
	@Override
	public List<Approval> getAllApproval(int... rowStartIdxAndCount) {
		return approvalDao.findAll(rowStartIdxAndCount);
	}

	@Override
	public void createApproval(Approval approval) {
		approvalDao.create(approval);
	}

	@Override
	public void deleteApproval(Long id) {
		approvalDao.delete(id);
	}

	@Override
	public void deleteApproval(Approval approval) {
		approvalDao.delete(approval);
	}

	@Override
	public void updateApproval(Approval approval) {
		approvalDao.update(approval);
	}

	@Override
	public Approval setApproval(User user, String url, long personaId,
			boolean alwaysAllow) {
		Persona persona = personaManager.getPersona(personaId);
		Approval approval = getApproval(user, url);
		if (approval == null) {
			Site site = siteManager.getSite(url);
			if (site == null) {
				site = new Site(url);
				siteManager.createSite(site);
			}
			approval = new Approval(site, user, persona, alwaysAllow);
			createApproval(approval);
		} else {
			approval.setPersona(persona);
			approval.setAlwaysAllow(alwaysAllow);
			updateApproval(approval);

		}

		return approval;
	}

}
